$(function(){
    //--------- Process for mask text fields ---------
    $('input.textMask, textarea.textMask').each(function(){
        var curSelector = '#' + $(this).attr('id');
        var refSelector = '#' + $(this).attr('ref');
        
        $(this).focus(function(){
            $(this).hide();
            $(refSelector).val('').show().focus();
        });
        
        $(refSelector).blur(function() {
            if($(this).val() === ""){
                $(this).hide();
                $(curSelector).show();
            }
        });
    });
    //--------- //Process for mask text fields ---------
    
    //----------- Menu -----------
    $('#menu a').hover(
        function () {
            var childMenu = $(this).next('.haveSub'),
                startX = 0,
                ownWidth = $(this).width(),
                ownPos = $(this).position();
            
            $(this).addClass('current');
            
            if ( childMenu.length ) {
                //calculate startX
                if ( (ownPos.left - 60) > 0) {
                    var tLength = 0;

                    childMenu.children('li').each(function(){
                        tLength += $(this).children('a').text().length;
                    });
                    tLength = (tLength * 4) + (childMenu.children('li').length * 10);
                    startX = (ownPos.left + ownWidth/2) - tLength;
                }

                childMenu.parent('li').addClass('active');
                childMenu.find('li:first').css('margin-left', startX + 'px');
                childMenu.slideDown('slow');
            }
        },
        function(){
            $(this).parent('li').mouseleave(function(){
                $(this).find('.haveSub').fadeOut();
                $(this).find('a.current').removeClass('current');
                $(this).removeClass('active');
            });
        }
    );
    //----------- //Menu -----------

    //----------- //Scroll-----------
    var scrolling = false;
    $(window).scroll(function(){
        if ( ! scrolling ) {
            if( $(window).scrollTop() <= 200 ) {
                $('#scrollTop').fadeOut("slow");
            } else {
                $('#scrollTop').fadeIn("slow");
            }
        }
    });
    $(".scrollTo").click(function() {
        var markedName = $(this).attr('href').replace('#', '');
        scrolling = true;
        
        $('html, body').animate({
            scrollTop: $("a[name='"+ markedName +"']").offset().top,
        }, 'slow', function(){
            $('#scrollTop').fadeIn("slow");
            scrolling = false;
        });
    });
    $('#scrollTop').click(
        function (e) {
            $('html, body').animate({
                scrollTop: 0
            }, 'slow');
        }
    );
    //----------- //Scroll-----------
    
    
});